package com.cloudfun.campusshare.device.protocol.decoder;

import com.cloudfun.campusshare.device.protocol.response.GgyAlarm;
import org.apache.commons.lang.math.NumberUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.BiConsumer;

/**
 * Created by Huqin on 2020/3/12
 */
public class GgyAlarmMessageDecoder extends ByteToMessageDecoder<GgyAlarm> {

    private List<FieldDecoder<GgyAlarm>> fieldDecoderList;

    public GgyAlarmMessageDecoder() {
        fieldDecoderList = new ArrayList<>();
        fieldDecoderList.add(new BitSwitchFieldDecoder<>(1, 1, this.alarm0Map()));
        fieldDecoderList.add(new BitSwitchFieldDecoder<>(0, 1, this.alarm1Map()));
    }

    @Override
    protected List<FieldDecoder<GgyAlarm>> fieldDecoderList() {
        return fieldDecoderList;
    }

    @Override
    protected GgyAlarm prepare() {
        return new GgyAlarm();
    }

    @Override
    protected boolean preProcessBeforeDecode(byte[] data, GgyAlarm obj) {
        for (byte byt : data) {
            if (NumberUtils.BYTE_ZERO != byt) {
                return false;
            }
        }
        obj.setAlarmCancel(true);
        return true;
    }

    public Map<Integer, BiConsumer<GgyAlarm, Boolean>> alarm1Map() {
        Map<Integer, BiConsumer<GgyAlarm, Boolean>> map = new HashMap<>();
        map.put(0, GgyAlarm::setTempSensorErr);
//        map.put(1, GgyAlarm::setTempSensorErr);
//        map.put(2, GgyAlarm::setTempSensorErr);
        map.put(3, GgyAlarm::setHeatingPipeErr);
        map.put(4, GgyAlarm::setPumpErr);
        map.put(6, GgyAlarm::setSciCommErr);
        return map;
    }

    public Map<Integer, BiConsumer<GgyAlarm, Boolean>> alarm0Map() {
        Map<Integer, BiConsumer<GgyAlarm, Boolean>> map = new HashMap<>();
        map.put(0, GgyAlarm::setDoorCloseErr);
        map.put(1, GgyAlarm::setMotorCommErr);
        map.put(2, GgyAlarm::setFilterBlockedAlarm);
        map.put(3, GgyAlarm::setMotorStopAlarm);
        map.put(4, GgyAlarm::setCoinInsertErr);
        map.put(5, GgyAlarm::setWaterBoxFullAlarm);
        return map;
    }
}
